package com.gmics.unifiq;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.gmics.android.R;
import com.gmics.unifiq.adapter.HomePager;
import com.gmics.unifiq.controller.UserFunctions;
import com.gmics.unifiq.menu.Account;
import com.gmics.unifiq.menu.FAQ;

public class MainActivity extends FragmentActivity implements
		ActionBar.TabListener {
	HomePager pagerAdapter;
	ViewPager viewPager;
	UserFunctions userFunctions;
	int i = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_pager);

		pagerAdapter = new HomePager(getSupportFragmentManager());
		final ActionBar actionBar = getActionBar();
		actionBar.setHomeButtonEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		viewPager = (ViewPager) findViewById(R.id.pager);
		viewPager.setAdapter(pagerAdapter);
		viewPager
				.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
					@Override
					public void onPageSelected(int position) {
						actionBar.setSelectedNavigationItem(position);
					}
				});

		for (int i = 0; i < pagerAdapter.getCount(); i++) {
			actionBar
					.addTab(actionBar.newTab()
							.setText(pagerAdapter.getPageTitle(i))
							.setTabListener(this));
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected (MenuItem item){
		if (item.getItemId() == R.id.action_settings){
			
		}
		else if (item.getItemId() == R.id.menu_account){              
			Intent accScreen = new Intent(this, Account.class);
			startActivity(accScreen);
		}
		else if (item.getItemId() == R.id.menu_faq){
			Intent faqScreen = new Intent(this, FAQ.class);
			startActivity(faqScreen);
		}else if(item.getItemId() == R.id.menu_findprofile){
			Intent findprofile = new Intent(this, FindFriend.class);
			startActivity(findprofile);
		}
		else if (item.getItemId() == R.id.menu_logout){

			// Check login status in database
			userFunctions = new UserFunctions();
			userFunctions.logoutUser(getApplicationContext());
			Intent login = new Intent(getApplicationContext(),
					LoginActivity.class);
			login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(login);
			// Closing dashboard screen
			finish();
		}

		return true;
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {

	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		viewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {

	}
	
	public int getCurrentFragment(){
		int index = viewPager.getCurrentItem();
		return index;
	}

}
